package g2.routemaster.gui.wizards;

import g2.routemaster.gui.general.EBonus;
import g2.routemaster.model.BonusTile;
import g2.routemaster.model.CoachCard;
import g2.routemaster.model.LengthTile;
import g2.routemaster.model.OutsideOfBaiern;
import g2.routemaster.model.Player;
import g2.routemaster.model.RegionTile;
import g2.routemaster.model.Spielender;

import java.util.Iterator;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;

public class ShowPlayerBonusesPage extends WizardPage {
   private Point place = new Point(10,5);

   protected ShowPlayerBonusesPage() {
      super("playerBonusesPage");
      setTitle("Bonuses");
      setDescription("Player bonuses");
   }

   public void createControl(Composite parent) {
      Composite composite = new Composite(parent, SWT.NONE);
      GridLayout layout = new GridLayout();
      layout.numColumns = 2;
      layout.verticalSpacing = 0;
      composite.setLayout(layout);

      Player player = getPlayer();
      CoachCard coachCard = player.getHighestCoachCard();
      if(coachCard != null){
      	//int length = coachCard.getLength();
      	final int value = coachCard.getValue();
      	
         Canvas canvas = new Canvas(composite, SWT.NONE);
         GridData gd = new GridData();
         gd.widthHint = 30;
         gd.heightHint = 30;
         gd.minimumHeight = 30;
         gd.minimumWidth = 30;
         canvas.setLayoutData(gd);
         
         canvas.addListener(SWT.Paint, new Listener(){
            public void handleEvent(Event e){
               drawCoachCard(e.gc, value);
            }
         });
         
         Label label = new Label(composite, SWT.NONE);
      	label.setText("CoachCard");
      }
      
      if(player.sizeOfBonusTiles() > 0){
         Iterator iter = player.iteratorOfBonusTiles();
         while(iter.hasNext()){
            BonusTile bonusTile = (BonusTile)iter.next();
            
            Canvas canvas = new Canvas(composite, SWT.NONE);
            GridData gd = new GridData();
            gd.widthHint = 30;
            gd.heightHint = 30;
            gd.minimumHeight = 30;
            gd.minimumWidth = 30;
            canvas.setLayoutData(gd);
            
            Label label = new Label(composite, SWT.NONE);
            if(bonusTile instanceof LengthTile){
               final LengthTile tile = (LengthTile)bonusTile;
               label.setText("Length Bonus(" + tile.getLength() + ")");
               canvas.addListener(SWT.Paint, new Listener(){
                  public void handleEvent(Event e){
                     drawLengthBonus(e.gc, 
                           tile.getLength(), tile.getValue());
                  }
               });
            } else if(bonusTile instanceof RegionTile){
               final RegionTile tile = (RegionTile)bonusTile;
               label.setText("Region Bonus(" + tile.getName() + ")");
               canvas.addListener(SWT.Paint, new Listener(){
                  public void handleEvent(Event e){
                     switch(EBonus.byNom(tile.getName())){
                        case BADEN:
                           drawRegionBonus(e.gc, 
                                 new RGB(183, 138, 240), 
                                 new RGB(183, 138, 240),
                                 tile.getValue());
                           break;
                        case BAIERN:
                           drawRegionBonus(e.gc, 
                                 new RGB(126, 184, 167), 
                                 new RGB(126, 184, 167),
                                 tile.getValue());
                           break;
                        case BOHMEN_SALZBURG:
                           drawRegionBonus(e.gc, 
                                 new RGB(201, 135, 142), 
                                 new RGB(232, 109, 166),
                                 tile.getValue());
                           break;
                        case SCHWEIZ_TYROL:
                           drawRegionBonus(e.gc, 
                                 new RGB(84, 138, 239), 
                                 new RGB(246, 72, 20),
                                 tile.getValue());
                           break;
                        case WYRTTEMBERG_HOHENZOLLERN:
                           drawRegionBonus(e.gc, 
                                 new RGB(128, 248, 242), 
                                 new RGB(62, 248, 135),
                                 tile.getValue());
                           break;
                     }
                  }
               });
            } else if(bonusTile instanceof OutsideOfBaiern){
               final OutsideOfBaiern tile = (OutsideOfBaiern)bonusTile;
               label.setText("Outside of Baiern Bonus");
               canvas.addListener(SWT.Paint, new Listener(){
                  public void handleEvent(Event e){
                     drawOutsideBaiern(e.gc, tile.getValue());
                  }
               });
            } else if(bonusTile instanceof Spielender){
               label.setText("Spielender");
               canvas.addListener(SWT.Paint, new Listener(){
                  public void handleEvent(Event e){
                     drawSpielender(e.gc);
                  }
               });
            } else {
               System.out.println("Bad bonus: " + bonusTile.getClass().getName() + " " + bonusTile.getValue());
               canvas.dispose();
               label.dispose();
            }
         }      
      } else {
         //new Label(composite, SWT.NONE).setText("None");
      }
      
      //composite.pack();
      
      // set the composite as the control for this page
      setControl(composite);
      setPageComplete(false);
   }

   Player getPlayer(){
      ShowPlayerBonusesWizard wizard = (ShowPlayerBonusesWizard)getWizard();
      return wizard.player;
   }
   
   
   
   Font getBonusFont(){
      Font font = Display.getCurrent().getSystemFont();
      FontData fontData = font.getFontData()[0];
      fontData.setStyle(SWT.BOLD);
      fontData.setHeight(12);
      return new Font(Display.getCurrent(), fontData);
   }
   
   Font getLengthFont(){
      Font font = Display.getCurrent().getSystemFont();
      FontData fontData = font.getFontData()[0];
      fontData.setStyle(SWT.BOLD);
      fontData.setHeight(8);
      return new Font(Display.getCurrent(), fontData);
   }
   
   void drawRegionBonus(GC gc, RGB color1, RGB color2, int value){
      // draw bonus
      gc.setForeground(new Color(Display.getCurrent(), color1));
      gc.setBackground(new Color(Display.getCurrent(), color2));
      gc.fillGradientRectangle(place.x-8, place.y-3, 25, 25, false);
      
      // draw line around rectangle
      gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
      gc.drawRectangle(place.x-8, place.y-3, 25, 25);
            
      // draw value
      gc.setFont(getBonusFont());
      gc.drawString(String.valueOf(value), place.x, place.y, true);
   }
   
   void drawLengthBonus(GC gc, int length, int value){
      // draw bonus
      gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
      gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
      gc.fillRectangle(place.x-8, place.y-3, 25, 25);
      
      // draw line around rectangle
      gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
      gc.drawRectangle(place.x-8, place.y-3, 25, 25);
      
      // draw value
      gc.setFont(getLengthFont());
      gc.drawString(String.valueOf(length), place.x+1, place.y-4, true);
      
      gc.drawLine(place.x-5, place.y+2, place.x, place.y+2);
      gc.drawLine(place.x+9, place.y+2, place.x+14, place.y+2);
      
      gc.setFont(getBonusFont());
      gc.drawString(String.valueOf(value), place.x, place.y+4, true);
   }
   
   void drawSpielender(GC gc){
      // draw bonus
      gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
      gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
      gc.fillGradientRectangle(place.x-8, place.y-3, 25, 25, false);
      
      // draw line around rectangle
      gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
      gc.drawRectangle(place.x-8, place.y-3, 25, 25);
            
      // draw value
      gc.setFont(getBonusFont());
      gc.drawString("S", place.x, place.y, true);
   }
   
   void drawCoachCard(GC gc, int value){
      // draw bonus
      gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
      gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
      gc.fillGradientRectangle(place.x-8, place.y-3, 25, 25, false);
      
      // draw line around rectangle
      gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
      gc.drawRectangle(place.x-8, place.y-3, 25, 25);
            
      // draw value
      gc.setFont(getBonusFont());
      gc.drawString(String.valueOf(value), place.x, place.y, true);
   }
   
   void drawOutsideBaiern(GC gc, int value){
      // draw bonus
      gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
      gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
      gc.fillGradientRectangle(place.x-8, place.y-3, 25, 25, false);
      
      // draw line around rectangle
      gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
      gc.drawRectangle(place.x-8, place.y-3, 25, 25);
            
      // draw value
      gc.setFont(getBonusFont());
      gc.drawString(String.valueOf(value), place.x, place.y, true);
   }
}
